<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class BaseController extends Controller
{
    /*
    * 无限递归
    */
    public function getTree($data,$id=0,$cengji=0)
    {
        static $getTreeData=array();
        foreach ($data as $k=>$v) {

            if($v['cate_pid']==$id){

                $v['cengji']=$cengji;
                $getTreeData[]=$v;

                $this->getTree($data,$v['cate_id'],$cengji+1);
            }
        }
        return $getTreeData;
    }

    /*
     * 获取所有子分类的 id
     */
    public function getChildren($data,$id,$cler=false)
    {
        static $dataChildren = array();
        if($cler){

            $dataChildren=array();
        }
        foreach($data as $k=>$v){
            if($v['cate_pid']==$id){
                $dataChildren[]=$v['cate_id'];
                $this->getChildren($data,$v['cate_id'],false);
            }
        }
        return $dataChildren;
    }

    /*
     * 上传单张图片的方法
     */
    public function UploadOneImage($file,$path)
    {
        $fileOne=Input::file($file);
        //获取上传文件的后缀名
        $ex=$fileOne->getClientOriginalExtension();
        //给上传图片命名
        $pic_name=time().rand(1000,9999).'.'.$ex;
        //拼装图片上传路径
        $pic_path="./admin/upload/".$path."/".date('Y-m-d').'/';
        //移动上传文件
        $fileOne->move($pic_path,$pic_name);
        //返回上传图片的名字
        $OneImgName=$pic_path.$pic_name;
        return $OneImgName;
    }
}
